package com.jc.gameconter.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

import com.jc.common.util.IPUtil;

@Controller
public class BaseController {
	public Logger getLoger() {
		return LoggerFactory.getLogger(this.getClass());
	}
	
	public String getRequestIP(HttpServletRequest request) {
		return IPUtil.getIPAddress(request);
	}
	
	
    public Map<String,Object> readParamToMap(HttpServletRequest request) throws UnsupportedEncodingException {

        BufferedReader br = null;
        StringBuilder sb = new StringBuilder("");
        request.setCharacterEncoding("utf8");
        try
        {
            br = request.getReader();
            String str;
            while ((str = br.readLine()) != null)
            {
                sb.append(str);
            }
            br.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (null != br)
            {
                try
                {
                    br.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        String[] split = sb.toString().split("&");
        Map<String,Object> result = new HashMap<>();
        for (String val : split){
            result.put(URLDecoder.decode(val.split("=")[0],"UTF-8"),URLDecoder.decode(val.split("=")[1],"UTF-8"));
        }
        return result;
    }
	

}
